package com.example.novelweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.common.ResultUtils;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.dto.wordrule.WordRuleAddRequest;
import com.example.novelweb.model.dto.wordrule.WordRuleDelRequest;
import com.example.novelweb.model.entity.Wordrule;
import com.example.novelweb.model.vo.UserVO;
import com.example.novelweb.service.PermissionService;
import com.example.novelweb.service.WordruleService;
import com.example.novelweb.mapper.WordruleMapper;
import com.example.novelweb.utils.UserUtils;
import com.example.novelweb.utils.WordCheck.WordCheckUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author PERSON
* @description 针对表【wordrule(违禁词规则)】的数据库操作Service实现
* @createDate 2025-02-14 19:17:24
*/
@Service
public class WordruleServiceImpl extends ServiceImpl<WordruleMapper, Wordrule>
    implements WordruleService{

  @Resource
  private PermissionService permissionService;

  @Override
  public BaseResponse<List<Wordrule>> listAll() {
    // 校验登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    // 权限鉴定: admin
    if(!permissionService.checkAdminRight(userVO)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    }
    List<Wordrule> list = query().list();
    return ResultUtils.success(list);
  }

  @Override
  @Transactional
  public BaseResponse<Boolean> addWords(WordRuleAddRequest wordRuleAddRequest) {
    // 校验登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    // 权限鉴定: admin
    if(!permissionService.checkAdminRight(userVO)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    }

    // DfaTree
    String list = wordRuleAddRequest.getWordList();
    Set<String> stringSet = Arrays.stream(list.split(","))
        .collect(Collectors.toSet());
    WordCheckUtil.addWords(stringSet);

    // sql
    int result = getBaseMapper().insertOrUpdateBatch(stringSet);
    ThrowUtils.throwIf(result<=0,ErrorCode.OPERATION_ERROR,"save failed");


    return ResultUtils.success(true);

  }

  @Override
  @Transactional
  public BaseResponse<Boolean> delWords(WordRuleDelRequest wordRuleDelRequest) {
    // 校验登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    // 权限鉴定: admin
    if(!permissionService.checkAdminRight(userVO)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    }


    // DfaTree
    String list = wordRuleDelRequest.getWordList();
    Set<String> stringSet = Arrays.stream(list.split(","))
        .collect(Collectors.toSet());
    WordCheckUtil.delWords(stringSet);

    // sql
    int result = getBaseMapper().updateStatusBatch(stringSet);
    ThrowUtils.throwIf(result<=0,ErrorCode.OPERATION_ERROR,"del failed");

    return ResultUtils.success(true);

  }



}




